package handlers

import (
	"encoding/json"
	"net/http"
	"shopback/models"
	"strconv"

	"github.com/gorilla/mux"
)

// AddComment 添加评论
	// @Summary 添加商品评论
	// @Description 用户对商品添加评论和评分
	// @Tags 评论
	// @Accept json
	// @Produce json
	// @Param comment body models.Comment true "评论信息"
	// @Success 201 {object} map[string]interface{} "评论添加成功"
	// @Failure 400 {string} string "请求参数错误"
	// @Failure 500 {string} string "服务器内部错误"
	// @Router /comments [post]
func AddComment(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	var comment struct {
		UserID    int    `json:"user_id"`
		ProductID int    `json:"product_id"`
		OrderID   int    `json:"order_id"`
		Content   string `json:"content"`
		Rating    int    `json:"rating"`
	}
	if err := json.NewDecoder(r.Body).Decode(&comment); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		json.NewEncoder(w).Encode(map[string]string{"error": "无效的请求数据"})
		return
	}

	commentID, err := models.AddComment(comment.UserID, comment.ProductID, comment.OrderID, comment.Content, comment.Rating)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
		return
	}

	w.WriteHeader(http.StatusCreated)
	json.NewEncoder(w).Encode(map[string]interface{}{
		"message":    "评论添加成功",
		"comment_id": commentID,
	})
}

// GetProductComments 获取商品评论
	// @Summary 获取商品评论
	// @Description 获取指定商品的所有评论
	// @Tags 评论
	// @Accept json
	// @Produce json
	// @Param product_id path int true "商品ID"
	// @Success 200 {object} map[string]interface{} "评论列表"
	// @Failure 400 {string} string "无效的商品ID"
	// @Failure 500 {string} string "服务器内部错误"
	// @Router /products/{product_id}/comments [get]
func GetProductComments(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	vars := mux.Vars(r)
	productID, err := strconv.Atoi(vars["product_id"])
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		json.NewEncoder(w).Encode(map[string]string{"error": "无效的商品ID"})
		return
	}

	comments, ReviewsGrade, ReviewsNumber, err := models.GetCommentsByProductID(productID)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
		return
	}

	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(map[string]interface{}{
		"comments":      comments,
		"reviewsgrade":  ReviewsGrade,
		"reviewsnumber": ReviewsNumber,
	})
}

// DeleteComment 删除评论
	// @Summary 删除评论
	// @Description 用户删除自己的评论
	// @Tags 评论
	// @Accept json
	// @Produce json
	// @Param comment_id path int true "评论ID"
	// @Param user_id path int true "用户ID"
	// @Success 200 {object} map[string]string "评论删除成功"
	// @Failure 400 {string} string "无效的评论ID"
	// @Failure 403 {string} string "无权删除该评论"
	// @Failure 500 {string} string "服务器内部错误"
	// @Router /comments/{comment_id}/users/{user_id} [delete]
func DeleteComment(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	commentID, err := strconv.Atoi(vars["comment_id"])
	if err != nil {
		http.Error(w, "无效的评论ID", http.StatusBadRequest)
		return
	}

	userID, err := strconv.Atoi(vars["user_id"])
	if err != nil {
		http.Error(w, "无效的用户ID", http.StatusBadRequest)
		return
	}

	if err := models.DeleteComment(commentID, userID); err != nil {
		if err.Error() == "评论不存在或无权删除" {
			http.Error(w, err.Error(), http.StatusForbidden)
			return
		}
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	json.NewEncoder(w).Encode(map[string]string{
		"message": "评论删除成功",
	})
}
